<template>
  <div class="my">
    <div class="img">
      <div class="bgc">
        <img src="http://liufusong.top:8080/img/profile/bg.png" alt="" />
      </div>
      <div class="login">
        <div class="top">
          <div class="avatar">
            <img :src="avatar" alt="">
            <p>{{name}}</p>
          </div>
        </div>
        <div class="bottom">
          <p @click="$router.push('/login')" v-if="isLogin">去登陆</p>
          <p @click="logout" v-else>退出</p>

        </div>
      </div>
    </div>

    <div class="body">
      <div class="item">
        <van-icon name="star-o" size="25px" @click="$router.push('/collect')"/>
        <p>我的收藏</p>
      </div>
      <div class="item">
        <van-icon name="wap-home-o" size="25px" @click="$router.push('/rent')"/>
        <p>我的出租</p>
      </div>
      <div class="item">
        <van-icon name="clock-o" size="25px"/>
        <p>看房记录</p>
      </div>
      <div class="item">
        <van-icon name="credit-pay" size="25px" @click="$router.push('/addHouse')"/>
        <p>成为房主</p>
      </div>
      <div class="item">
        <van-icon name="user-o" size="25px"/>
        <p>个人资料</p>
      </div>
      <div class="item">
        <van-icon name="service-o" size="25px"/>
        <p>联系我们</p>
      </div>
    </div>

    <div class="footer">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="">
    </div>
  </div>
</template>

<script>
import { getUserInfo, userLogout } from '@/api/user'
import { delToken, getToken } from '@/utils/storage'
export default {
  name: 'my-page',
  data () {
    return {
      user: {},
      avatar: 'https://gimg3.baidu.com/search/src=https%3A%2F%2Fbkimg.cdn.bcebos.com%2Fsmart%2Fc8ea15ce36d3d539b60031230ccefe50352ac75c1ab0-bkimg-process%2Cv_1%2Crw_1%2Crh_1%2Cmaxl_300%2Cpad_1&refer=http%3A%2F%2Fwww.baidu.com&app=2021&size=f256,256&n=0&g=0n&q=100&fmt=auto?sec=1668013200&t=e184256afc46968868fdcbf63b6fe157',
      isLogin: true,
      name: '小桃子'
    }
  },
  async created () {
    const res = await getUserInfo()
    console.log(res)
    const token = getToken()
    if (token && res.status !== 400) {
      this.user = res.body
      this.avatar = 'http://liufusong.top:8080' + res.body.avatar
      this.name = res.body.nickname
      if (res.status === 200) {
        this.isLogin = false
      }
    }
  },
  methods: {
    async logout () {
      await userLogout()
      this.$toast.success('退出登录成功')
      delToken()
      setTimeout(() => {
        this.$router.push('/login')
      }, 500)
    }
  }
}
</script>

<style lang="less" scoped>
.my {
  overflow: hidden;
  .img {
    position: absolute;
    height: 150px;
    .login {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: -100px;
      width: 280px;
      height: 150px;
      background-color: #fff;
      box-shadow: 0 0 10px 3px #ddd;
      .top{
        position: relative;
        .avatar {
          img {
            position: absolute;
            top: -30px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 999;
            width: 60px;
            height: 60px;
            border-radius: 50%;
          }
          p {
           position: absolute;
           top: 30px;
           left: 50%;
           transform: translateX(-50%);
           font-size: 14px;
          }
        }

      }
      .bottom {
        margin-top: 90px;
        margin-left: 102px;
        width: 70px;
        height: 30px;
        background-color: pink;
        text-align: center;
        line-height: 30px;
        border-radius: 5px;
        p{
          color: #fff;
        }
      }
    }
  }
  .body {
    margin-top: 300px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    align-items: center;
    .item {
      width: 100px;
      height: 100px;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }
  .footer {
    img {
      margin-left: 15px;
      width: 345px;
    }
  }
}
</style>
